import os.path
import time

import psutil

from utils.linux_helper import LocalLinuxHelper
from utils.logger_helper import LoggerHelper

app_name = "mysql_checker"
logger = LoggerHelper.get_logger(logger_name=app_name)


class MySQLChecker(object):
    def check_long_trans(self, long_tran_seconds: int = 3):
        """
        检查长事务,默认执行超过3秒的为长事务
        :param long_tran_seconds:
        :return:
        """
        pass

    def check_long_queries(self, long_query_seconds: int = 3):
        """
        检查长查询,默认执行超过3秒的为长查询
        :param long_query_seconds:
        :return:
        """
        pass

    @classmethod
    def get_log_dir(cls):
        base_dir = os.path.dirname(__file__)
        log_dir = os.path.join(base_dir, "logs")
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        return log_dir

    @classmethod
    def check_io(cls, check_times: int = 10):
        cmd_temp = """
        iostat -dxk 1 {check_times} |awk '{ print strftime("%Y-%m-%d %H:%M:%S", systime()),$0}' > {log_path} &
        """
        log_path = os.path.join(cls.get_log_dir(), "io_usage.txt")
        cmd = cmd_temp.format(
            check_times=check_times,
            log_path=log_path
        )
        LocalLinuxHelper.exec_cmd(cmd=cmd)

    @classmethod
    def check_cpu(cls, check_times: int = 10):
        cmd_temp = """
        /usr/bin/vmstat -w 1 {check_times} |awk '{ print strftime("%Y-%m-%d %H:%M:%S", systime()),$0}' > {log_path} &
        """
        log_path = os.path.join(cls.get_log_dir(), "cpu_usage.txt")
        cmd = cmd_temp.format(
            check_times=check_times,
            log_path=log_path
        )
        LocalLinuxHelper.exec_cmd(cmd=cmd)

    @classmethod
    def check_memory(cls, check_times: int = 10):
        cmd_temp = "/usr/bin/iostat -dxk 1 {check_times} > {log_path} &"
        log_path = os.path.join(cls.get_log_dir(), "iostat.txt")
        cmd = cmd_temp.format(
            check_times=check_times,
            log_path=log_path
        )
        LocalLinuxHelper.exec_cmd(cmd=cmd)

    @classmethod
    def get_cpu_info(cls):
        cpu_info = psutil.cpu_times()
        psutil.disk_io_counters()
        psutil.net_io_counters(pernic=True)
        disk_partitions = psutil.disk_partitions()
        for disk_partition in disk_partitions:
            dick_usage = psutil.disk_usage(path=disk_partition.device)

        psutil.di

    @classmethod
    def check_server(cls):
        cpu_info_dict = dict()
        disk_io_dict = dict()
        net_io_dict = dict()
        for check_index in range(0, 10):
            cpu_info_dict[check_index] = {
                "check_time": time.time(),
                "cpu_times": psutil.cpu_times()
            }
            disk_io_dict[check_index] = {
                "check_time": time.time(),
                "io_counters": psutil.disk_io_counters()
            }
            net_io_dict[check_index] = {
                "check_time": time.time(),
                "io_counters": psutil.net_io_counters(pernic=True)
            }


def get_dick_usage():
    disk_partitions = psutil.disk_partitions()
    for disk_partition in disk_partitions:
        dick_usage = psutil.disk_usage(path=disk_partition.mountpoint)
        print("{}:{}".format(disk_partition.mountpoint, dick_usage))
    pass

